//
//  ProductViewController.m
//  Spa
//
//  Created by PangPond on 10/1/2556.
//  Copyright (c) พ.ศ. 2556 innova. All rights reserved.
//

#import "ProductViewController.h"
#import "AddProductViewController.h"
#import "Product.h"
@interface ProductViewController ()

@end

@implementation ProductViewController
@synthesize product = _product;
@synthesize tableView = _tableView;
@synthesize keyDelete;
@synthesize databaseUtility =_databaseUtility;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self loadView];
    [self viewDidLoad];
}

- (BOOL) hidesBottomBarWhenPushed {
    return YES;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.hidesBottomBarWhenPushed = YES;
    self.databaseUtility = [[DatabaseUtility alloc]initWithDatabaseName:@"Database"];
    self.product = [self.databaseUtility fetchedResultsWithClass:@"Product"withSortDescriptorString:@"name"];
    [self.tableView reloadData];
    UIBarButtonItem * settingButton = [[UIBarButtonItem alloc] initWithTitle:@"Add" style:UIBarButtonItemStylePlain target:self action:@selector(AddProduct:)];
    self.navigationItem.rightBarButtonItem = settingButton;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.product count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //////
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    //config xell

    Product * products = [self.product objectAtIndex:indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@",products.name];
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AddProductViewController *addProduct = [[AddProductViewController alloc] initWithNibName:@"AddProductViewController" bundle:nil];
    Product * products = [self.product objectAtIndex:indexPath.row];
    addProduct.keyProductName = products.name;
    addProduct.keyProductPrice = products.price;
    addProduct.keyIf = @"1";
    [self presentViewController:addProduct animated:YES completion:nil];
}


//funtion



-(IBAction)AddProduct:(id)sender{
    AddProductViewController *addProduct = [[AddProductViewController alloc] initWithNibName:@"AddProductViewController" bundle:nil];
    addProduct.keyIf = @"2";
    [self presentViewController:addProduct animated:YES completion:nil];
    
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    Product * productKey = [self.product objectAtIndex:indexPath.row];
    keyDelete = productKey.name;
    
    NSArray *products = [self.databaseUtility fetchedResultsWithClass:@"Product" withSortDescriptorString:@"name" withPredicateString:[NSString stringWithFormat:@"name == '%@'",keyDelete]];
    
    Product *product = [products lastObject];
    [self.databaseUtility.managedObjectContext deleteObject:product];
    [self.databaseUtility saveContext];
    [self viewDidLoad];
}
@end
